IF EXISTS (SELECT * FROM sysobjects WHERE type = 'P' AND name = 'spRequests_RetrieveAll')
	BEGIN
		PRINT 'Dropping Procedure spRequests_RetrieveAll'
		DROP  Procedure  spRequests_RetrieveAll
	END

GO

PRINT 'Creating Procedure spRequests_RetrieveAll'
GO

CREATE Procedure spRequests_RetrieveAll
	(
		@Request_ID int = null, 
		@CreatedByUser_ID numeric(9,0) = null, 
		@RequestDate DateTime = null, 
		@RequestStatus varchar(50) = null, 
		@Comments varchar(50) = null, 
		@ApprovedByUser_ID numeric(9,0) = null, 
		@ApprovedDate DateTime = null, 
		@OpenByUser_ID numeric(9,0) = null, 
		@OpenDate DateTime = null, 
		@ClosedByUser_ID numeric(9,0) = null, 
		@ClosedDate DateTime = null, 
		@DepartmentName varchar(50) = null, 
		@DivisionName varchar(50) = null
	)
AS
    /* 
    ------------------------------------------------------------------------------
    - Change History
    - --------------
    - Date          Description
    ------------------------------------------------------------------------------
    - 2007/11/02	Initial version created
    ------------------------------------------------------------------------------
    */
	
	SET NOCOUNT ON
	
	-- Retrieve the relevant records
	SELECT  *
	FROM	Requests
	
	WHERE	(@Request_ID IS NULL OR Requests.Request_ID = @Request_ID)
	  AND	(@CreatedByUser_ID IS NULL OR Requests.CreatedByUser_ID = @CreatedByUser_ID)
	  AND	(@RequestDate IS NULL OR Requests.RequestDate = @RequestDate)
	  AND	(@RequestStatus IS NULL OR Requests.RequestStatus LIKE @RequestStatus + '%')
	  AND	(@Comments IS NULL OR Requests.Comments LIKE @Comments + '%')
	  AND	(@ApprovedByUser_ID IS NULL OR Requests.ApprovedByUser_ID = @ApprovedByUser_ID)
	  AND	(@ApprovedDate IS NULL OR Requests.ApprovedDate = @ApprovedDate)
	  AND	(@OpenByUser_ID IS NULL OR Requests.OpenByUser_ID = @OpenByUser_ID)
	  AND	(@OpenDate IS NULL OR Requests.OpenDate = @OpenDate)
	  AND	(@ClosedByUser_ID IS NULL OR Requests.ClosedByUser_ID = @ClosedByUser_ID)
	  AND	(@ClosedDate IS NULL OR Requests.ClosedDate = @ClosedDate)
	  AND	(@DepartmentName IS NULL OR Requests.DepartmentName LIKE @DepartmentName + '%')
	  AND	(@DivisionName IS NULL OR Requests.DivisionName LIKE @DivisionName + '%')

	-- Return to calling method
	RETURN
GO
